// pages/cart/index.js
import {showModal , showToast} from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cartList: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  //添加收货地址
  //wx.chooseAddress(Object object) 获取用户收货地址。调起用户编辑收货地址原生界面，并在编辑完成后返回用户选择的地址
  handleChooseAddress() {
    wx.chooseAddress({
      success: (result) => {
        // console.log(result);
        wx.setStorageSync("address", result);
      },
      fail: () => { },
      complete: () => { }
    });
  },
 //随便看看
  letMeSeeSee(){
    wx.switchTab({
      url: '/pages/category/index',
    })
  },
  //设置购物车状态 并重新计算总价格 总数量 全选
  setCart(cartList) {
    let allChecked = true;
    let totalPrice = 0;
    let totalNum = 0;
    cartList.forEach(item => {
      if (item.checked) {
        totalNum += item.goods_num;
        totalPrice += item.goods_price * item.goods_num
      } else {
        allChecked = false;
      }
    });
    allChecked = cartList.length != 0 ? allChecked : false;
    this.setData({
      cartList,
      allChecked,
      totalNum,
      totalPrice
    });
    //把更新后的数据再塞进去
    wx.setStorageSync("cart", cartList);
  },

  //选中复选框
  handleChk(e) {
    console.log(e.currentTarget.dataset.id);
    let { id } = e.currentTarget.dataset;
    let { cartList } = this.data;
    //找到被修改的商品
    let index = cartList.findIndex(v => v.goods_id === id);
    //选中状态取反
    cartList[index].checked = !cartList[index].checked;
    this.setCart(cartList)
  },

  //选中全选框
  handleAllChk() {
    let { cartList, allChecked } = this.data;
    allChecked = !allChecked;
    cartList.forEach(v => v.checked = allChecked);
    this.setCart(cartList)
  },

  //点击 + - 事件
  async handleClick(e) {
    let { id, mark } = e.currentTarget.dataset;
    let { cartList } = this.data;
    let index = cartList.findIndex(v => v.goods_id === id);

    if (cartList[index].goods_num === 1 && mark === -1) {
      //弹窗
      const res= await showModal({content:"是否删除该商品？"});
      if (res.confirm) {
        console.log('用户点击确定')
        cartList.splice(index,1);
        this.setCart(cartList)
      }
    } else {
      //修改数量
      cartList[index].goods_num += mark;
      this.setCart(cartList);
    }
  },

  //结算
  async handlePay(){
    const {address,totalNum} = this.data;
    if(Object.keys(address).length===0){
      await showToast({title:"您还没有添加收货地址"});
      return;
    }
    if(totalNum===0){
      await showToast({title:"您还没有选择商品哦"});
      return;
    }
    wx.navigateTo({
      url: '/pages/pay/index',
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  //every 数组方法会遍历会接收一个回调函数那么每一个回调函数都返回true那么every方法的返回值为true
  //只要有一个回调函数返回了false那么不再循环执行，直接返回false
  onShow: function () {
    let address = wx.getStorageSync("address");
    let cartList = wx.getStorageSync("cart") || [];

    this.setData({
      address,
    });
    this.setCart(cartList);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})